package com.eqdd.yiqidian.ui.mine.shenqing

import android.content.Intent
import com.eqdd.nextinputs.StaticScheme
import com.eqdd.yiqidian.base.Config
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditEnterBean
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimEditHintBean
import com.eqdd.yiqidian.bean.rebuild.slimedit.SlimTimeSelectBean
import com.eqdd.yiqidian.http.HttpConfig
import com.eqdd.yiqidian.ui.select.SelectRichTextActivity
import com.eqdd.yiqidian.widget.ActionSheetDialog
import com.lzy.okgo.model.HttpParams
import org.jetbrains.anko.startActivityForResult
import java.io.File

/**
 * 请假申请
 * Created by jhj on 18-6-11.
 */
class ApplyLeaveActivity : BaseApplyActivity() {

    private var mediaList: ArrayList<String>? = null
    private var text: String? = null


    override val url: String
        get() = HttpConfig.BASE_URL + HttpConfig.ADD_LEAVE
    override val mTitle: String
        get() = "请假申请"
    override val isRequestLeader: Boolean
        get() = true
    override val leaderIndex: Int?
        get() = 9

    override fun itemClick(p: Int) {
        when (p) {
            4 -> {
                isUpper = true
                openTimePicker(4, 5, true)
            }
            5 -> {
                isUpper = false
                openTimePicker(4, 5, true)
            }
            8 -> {
                startActivityForResult<SelectRichTextActivity>(Config.SELECT_RICH_TEXT, "content" to text,
                        Config.TITLE to "请假原因", "list" to mediaList)
            }
            7 -> bottomDialog(7, 33)
        }
    }


    override fun uploadParams(): HttpParams {
        params.put("leadvStartTime", setParam(4))
        params.put("leadvStartTime", setParam(4))
        params.put("leaveEndTime", setParam(5))
        params.put("leaveTime", setParam(6))
        params.put("leaveType", setParam(7))
        params.put("leaveReason", setParam(8))
        mediaList?.forEachIndexed { index, s ->
            params.put("file$index", File(s))
        }
        return params
    }

    override fun setEditBeans() {
        super.setEditBeans()
        datas.add(SlimTimeSelectBean("请假开始时间", "", 4, "请选择").setStartTime(true).setNeed(true))
        datas.add(SlimTimeSelectBean("请假结束时间", "", 5, "请选择").setStartTime(false).setNeed(true))
        datas.add(SlimEditEnterBean("请假天数", "", 6, "请输入").addSchemes(StaticScheme.Numeric()).setNeed(true))
        datas.add(SlimEditEnterBean("请假类型", "", 7, true, "请选择").setNeed(true))
        datas.add(SlimEditEnterBean("请假原因", "", 8, true, "请输入").setNeed(true))
        datas.add(SlimEditHintBean("审核人", "", 9))

    }

    override fun checkParams(): Boolean {
        if ((setParam(7) == "婚假" || setParam(7) == "产假") && (mediaList == null || mediaList?.size == 0)) {
            msg = setParam(7) + "需要照片证明"
            return false
        }
        return super.checkParams()
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (data == null)
            return
        if (resultCode == Config.SUCCESS) {
            when (requestCode) {
                Config.SELECT_RICH_TEXT -> {
                    mediaList = data.getSerializableExtra("list") as ArrayList<String>
                    text = data.getStringExtra("content")
                    slimAdapterEx.data[8].content = text
                    slimAdapterEx.notifyItemChanged(8)
                }
            }

        }

    }
}